
package com.rxw.nutmall.utils;

import com.rxw.nutmall.exception.ErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;

/**
 * 响应数据
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
@Setter
@Getter
@ApiModel(value = "响应")
public class Result<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    public static final int SUCCESS = 0;

    public static final String SUCCESS_MSG = "success";

    /**
     * 编码：0表示成功，其他值表示失败
     */
    @ApiModelProperty(value = "编码：0表示成功，其他值表示失败")
    private int code;
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "消息内容")
    private String msg;
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(SUCCESS, SUCCESS_MSG,data);
    }

    public  static <T> Result<T> success() {
        return new Result<>(SUCCESS, SUCCESS_MSG,null);
    }

    public  static <T> Result<T> success(String msg) {
        return new Result<>(SUCCESS, msg,null);
    }

    public static <T> Result<T> error() {
        return new Result<>(ErrorCode.INTERNAL_SERVER_ERROR,"服务器内部异常", null);
    }

    public static <T> Result<T> error(int code) {
        return new Result<>(code,"服务器内部异常", null);
    }

    public static <T> Result<T> error(int code, String msg) {
        return new Result<>(code,msg, null);
    }

    public static <T> Result<T> error(int code, String msg, T data) {
        return new Result<>(code,msg, data);
    }

    public static <T> Result<T> error(String msg) {
        return new Result<>(ErrorCode.INTERNAL_SERVER_ERROR,msg, null);
    }
}
